package com.rdtravel.services;

import com.rdtravel.entities.DepartureLocation;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
class JPADepartureLocationsService extends JPAService implements
		DepartureLocationsService {
	@Override
	public List<String> findNamesStartingWith(String term) {
		if (term.isEmpty()) {
			throw new IllegalArgumentException("term is empty");
		}
		TypedQuery<String> query = entityManager.createNamedQuery(
				"findDepartureLocationdNamesStartingWith", String.class);
		query.setParameter("wildCardPattern", term + '%');
		return query.getResultList();
	}

	@Override
	public DepartureLocation findByName(String name) {
		if (name.isEmpty()) {
			throw new IllegalArgumentException("name is empty");
		}
		TypedQuery<DepartureLocation> query = entityManager.createNamedQuery(
				"findDpartureLocationsByName", DepartureLocation.class);
		query.setParameter("name", name);
		try {
			return query.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}

}
